﻿using HuiZe.education.Resource.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// Win_MyShare_Download.xaml 的交互逻辑
    /// </summary>
    public partial class Win_MyShare_Download : HuiZeWindow
    {
        public string ErrorMessage { get; set; }

        public Win_MyShare_Download(string _url, string _path)
        {
            InitializeComponent();
            _path_save = _path;
            _url_download = _url;
            Loaded += Win_MyShare_Download_Loaded;
        }

        void Win_MyShare_Download_Loaded(object sender, RoutedEventArgs e)
        {
            _is_downloading = true;
            PART_Progress.Value = 0;
            PART_Progress.Maximum = 100;
            PART_Message.Text = $"(0%)正在下载“{Path.GetFileName(_url_download)}”...";

            new Thread(new ThreadStart(() =>
            {
                _client = new System.Net.WebClient();
                try
                {
                    _client.Proxy = null;
                    _client.DownloadProgressChanged += (s, args) =>
                    {
                        if(_is_downloading)
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                PART_Progress.Value = args.ProgressPercentage;
                                PART_Message.Text = $"({args.ProgressPercentage}%)正在下载“{Path.GetFileName(_url_download)}”...";
                            }));
                        }
                    };
                    _client.DownloadFileCompleted += (s, args) =>
                    {
                        if(_is_downloading)
                        {
                            _is_downloading = false;
                            Dispatcher.Invoke(new Action(() =>
                            {
                                if (args.Error == null)
                                {
                                    this.DialogResult = new bool?(true);
                                }
                                else
                                {
                                    ErrorMessage = args.Error.Message;
                                    this.DialogResult = new bool?(false);
                                }
                            }));
                        }
                    };
                    _client.DownloadFileAsync(new Uri(_url_download, UriKind.RelativeOrAbsolute), _path_save);
                }
                catch (Exception ex)
                {
                    App.Log(ex);
                }
            }))
            { IsBackground = true }.Start();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_client != null && _is_downloading)
            {
                _is_downloading = false;
                try
                {
                    _client.CancelAsync();
                    _client.Dispose();
                    _client = null;
                }
                catch (Exception ex)
                {
                    App.Log(ex);
                }
            }
            base.OnClosing(e);
        }


        string _path_save;
        string _url_download;
        bool _is_downloading;
        System.Net.WebClient _client;
    }
}
